package com.sweets.look.ui.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sweets.look.R;
import com.sweets.look.util.ViewUtils;

public class MarkDialog {
    private final Display display;
    private final Context mContext;
    private Dialog mDialog;
    private TextView mDialogMsg;
    private TextView mDialogTitle;
    private TextView mNegativeBtn;
    private TextView mNeutralBtn;
    private TextView mPositiveBtn;
    private ScrollView mScrollMsg;

    public MarkDialog(Context context) {
        super();
        this.mContext = context;
        this.display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    }


    public MarkDialog builder() {
        View localView = LayoutInflater.from(this.mContext).inflate(R.layout.widget_dialog,
                (ViewGroup) ((Activity) this.mContext).findViewById(android.R.id.content), false);//16908290
        RelativeLayout localRelativeLayout = (RelativeLayout) localView.findViewById(R.id.dialog_layout);
        this.mDialogTitle = ((TextView) localView.findViewById(R.id.dialog_title));
        this.mDialogTitle.setVisibility(View.GONE);
        this.mScrollMsg = ((ScrollView) localView.findViewById(R.id.msg_scroll));
        this.mScrollMsg.setVisibility(View.GONE);
        this.mDialogMsg = ((TextView) localView.findViewById(R.id.dialog_msg));
        this.mPositiveBtn = ((TextView) localView.findViewById(R.id.dialog_btn_positive));
        this.mPositiveBtn.setVisibility(View.GONE);
        this.mNegativeBtn = ((TextView) localView.findViewById(R.id.dialog_btn_negative));
        this.mNegativeBtn.setVisibility(View.GONE);
        this.mNeutralBtn = ((TextView) localView.findViewById(R.id.dialog_btn_neutral));
        this.mNeutralBtn.setVisibility(View.GONE);
        this.mDialog = new Dialog(this.mContext, R.style.DialogTheme);
        this.mDialog.setContentView(localView);
        int v4 = this.display.getWidth();
        int v0 = this.display.getHeight();
        int v2 = v4 >= v0 ? v0 / 6 * 5 : v4 / 6 * 5;
        localRelativeLayout.setLayoutParams(new FrameLayout.LayoutParams(v2, -2));
        this.mDialog.getWindow().setWindowAnimations(R.style.DialogAnimation);
        return this;
    }

    public void dismiss() {
        if (this.mDialog != null && (this.mDialog.isShowing())) {
            this.mDialog.dismiss();
        }
    }

    public MarkDialog setCancelable(boolean cancel) {
        this.mDialog.setCancelable(cancel);
        return this;
    }

    public MarkDialog setCanceledOnTouchOutside(boolean cancel) {
        this.mDialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public MarkDialog setMessage(String msg) {
        this.mDialogMsg.setText(msg);
        this.mScrollMsg.setVisibility(View.VISIBLE);
        int v1 = ViewUtils.getWindowHeight(this.mContext) - ViewUtils.convertToDensityPixels(this.mContext,
                200);
        if (ViewUtils.measureTextViewHeight(this.mContext, msg, 16) >= v1) {
            ViewGroup.LayoutParams v0 = this.mScrollMsg.getLayoutParams();
            v0.height = v1;
            this.mScrollMsg.setLayoutParams(v0);
        }

        return this;
    }

    public MarkDialog setNegativeButton(String text, final View.OnClickListener listener) {
        this.mNegativeBtn.setVisibility(View.VISIBLE);
        this.mNegativeBtn.setText(((CharSequence) text));
        this.mNegativeBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v);
                }

                MarkDialog.this.mDialog.dismiss();
            }
        });
        return this;
    }

    public MarkDialog setNeutralButton(String text, final View.OnClickListener listener) {
        this.mNeutralBtn.setVisibility(View.VISIBLE);
        this.mNeutralBtn.setText(text);
        this.mNeutralBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v);
                }

                MarkDialog.this.mDialog.dismiss();
            }
        });
        return this;
    }

    public MarkDialog setPositiveButton(String text, final View.OnClickListener listener) {
        this.mPositiveBtn.setVisibility(View.VISIBLE);
        this.mPositiveBtn.setText(text);
        this.mPositiveBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v);
                }

                MarkDialog.this.mDialog.dismiss();
            }
        });
        return this;
    }

    public MarkDialog setTitle(String title) {
        this.mDialogTitle.setVisibility(View.VISIBLE);
        this.mDialogTitle.setText(title);
        return this;
    }

    public void show() {
        this.mDialog.show();
    }
}

